package com.hq.fxh.No5_User.MyModle.Activity.item;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hq.fxh.CustomerView.switchbutton.SwitchButton;
import com.hq.fxh.No5_User.MyModle.AdBannerInfo;
import com.hq.fxh.No5_User.MyModle.ClearableEidtText;
import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;


/**
 * @author 大圣
 * @ClassName QRCodeAdItem
 * @Description : 二维码广告
 * @Created 2015/11/13 0013
 * @Email qwqe601660957@163.com
 */
public class QRCodeAdItem extends LinearLayout  {
    private FrameLayout flQRCodePicAdd;
    private ImageButton ibClose;
    private ImageView ivAd;
    private TextView tvAdTypeName;
    private ClearableEidtText etWechatId;
    private ClearableEidtText etDes;
    private TextView tvDes;
    private SwitchButton sbSwitch;
    private TextView tvSwitchInfo;
  //  private MerchantToolSysConfigPresenter merchantToolSysConfigPresenter;
    private AdBannerInfo adBannerInfo;
    private AdBannerInfo.AdvertDetailBean Advert_detail;
    private TextView pic_hint;

    public QRCodeAdItem(Context context) {
        super(context);
        inflate(context, R.layout.item_qrcode_ad, this);
        findView();
        etWechatId.setSingleLine(true);
        etWechatId.setHorizontallyScrolling(true);

        etDes.setSingleLine(true);
        etDes.setHorizontallyScrolling(true);

        tvAdTypeName.setText("二维码广告");
        pic_hint.setText("上传二维码");


    }

    private SpannableString getClickableSpan(String des, final String url) {
        OnClickListener l = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("title","二维码广告");
                bundle.putString("url", url);
              //  IntentUtils.openActivity(getContext(), WebActivity.class, bundle);
//                Toast.makeText(getContext(), "Click Success", Toast.LENGTH_SHORT).show();
            }
        };
        SpannableString spanableInfo = new SpannableString(des + "查看详情");
        int start = spanableInfo.toString().indexOf("查看详情");
        int end = spanableInfo.length();
        spanableInfo.setSpan(new Clickable(l), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanableInfo.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.blue)), start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanableInfo;
    }




    class Clickable extends ClickableSpan implements OnClickListener {
        private final OnClickListener mListener;

        public Clickable(OnClickListener l){
            mListener = l;
        }

        @Override
        public void onClick(View v){
            mListener.onClick(v);
        }
    }





    public void findView(){
        flQRCodePicAdd = (FrameLayout) findViewById(R.id.flQRCodePicAdd);
        ibClose = (ImageButton) findViewById(R.id.ibClose);
        ivAd = (ImageView) findViewById(R.id.ivAd);
        tvAdTypeName = (TextView) findViewById(R.id.tvAdTypeName);
        etWechatId = (ClearableEidtText) findViewById(R.id.etWechatId);
        etDes = (ClearableEidtText) findViewById(R.id.etDes);
        tvDes = (TextView) findViewById(R.id.tvDes);
        sbSwitch = (SwitchButton) findViewById(R.id.sbSwitch);
        tvSwitchInfo = (TextView) findViewById(R.id.tvSwitchInfo);
        pic_hint =(TextView)findViewById(R.id.pic_hint);
    }

    public void setAdInfo(final AdBannerInfo info){
        adBannerInfo = info;
        Advert_detail = info.getAdvert_detail();
        if (Advert_detail != null) {
            etWechatId.setText(Advert_detail.getName());
            etWechatId.setFilters(new InputFilter[]{new EmojiFilter()});
            etDes.setText(Advert_detail.getDes());
            etDes.setFilters(new InputFilter[]{new EmojiFilter()});
       //     ImageLoader.getInstance().load(getContext(), ivAd, Advert_detail.getPic());
            String pic = Advert_detail.getPic();
            if (pic == null || pic.equals("")) {

            } else {
                GlideUtils.loadImageView(getContext(), pic, ivAd);

            }        } else if (Advert_detail == null) {
            Advert_detail = new AdBannerInfo.AdvertDetailBean();
            info.setAdvert_detail(Advert_detail);
        }
        etWechatId.addTextChangedListener(new TextWatcher() {
            private int maxLen = 24;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = etWechatId.getText();
                int len = editable.length();
                if (len > maxLen) {
                    ToastUtils.show(getContext(), "输入字符不能超过"+maxLen+"个");
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, maxLen);
                    etWechatId.setText(newStr);
                    editable = etWechatId.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.setName(s.toString());
                info.setAdvert_detail(Advert_detail);
            }
        });
        etDes.addTextChangedListener(new TextWatcher() {
            private int maxLen = 50;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = etDes.getText();
                int len = editable.length();
                if (len > maxLen) {
                    ToastUtils.show(getContext(), "输入字符不能超过"+maxLen+"个");
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, maxLen);
                    etDes.setText(newStr);
                    editable = etDes.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);

                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.setDes(s.toString());
                info.setAdvert_detail(Advert_detail);
            }
        });
        sbSwitch.setChecked(BooleanUtils.intToBool(info.getIs_carousel()));
        setSbSwitchText(sbSwitch.isChecked(),0);
        sbSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                info.setIs_carousel(BooleanUtils.BoolToInt(isChecked));

            }
        });


    }

    public void setSbSwitchText(boolean isCheck,int  checkShow){
        if(isCheck){
            tvSwitchInfo.setTextColor(this.getResources().getColor(R.color.new_black));
            tvSwitchInfo.setTextSize(14);
            tvSwitchInfo.setText(R.string.ad_on_tip);
            if(checkShow > 0 ){
                ToastUtils.show(getContext(), R.string.to_show);
            }

        }else{
            tvSwitchInfo.setTextColor(this.getResources().getColor(R.color.new_black));
            tvSwitchInfo.setTextSize(14);
            tvSwitchInfo.setText(R.string.ad_off_tip);
            if(checkShow > 0){
                ToastUtils.show(getContext(), R.string.to_hide);
            }

        }
    }

    public void setOnClickListener(final OnClickListener listener){
        ibClose.setOnClickListener(listener);
        ivAd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(flQRCodePicAdd);
            }
        });
        flQRCodePicAdd.setOnClickListener(listener);
    }
    public void setImage(String url) {
     //   ImageLoader.getInstance().load(getContext(), ivAd, url);
        GlideUtils.loadImageView(getContext(),url,ivAd);

        if (adBannerInfo != null && adBannerInfo.getAdvert_detail() != null) {
            adBannerInfo.getAdvert_detail().setPic(url);
        } else {
            AdBannerInfo.AdvertDetailBean advertDetailBean = new AdBannerInfo.AdvertDetailBean();
            advertDetailBean.setPic(url);
            advertDetailBean.setName("");
            advertDetailBean.setDes("");
            adBannerInfo.setAdvert_detail(advertDetailBean);
        }
    }
}
